package com.common.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 录音处理
 */
public class RecordUtils {

    private static String FILE_START="D:\\yijie\\";

    /**
     * 下载网络录音，存储到本地，返回本地地址
     */
    public static String getLocalPath(String path,String folwId,String userId){
        String localPath="D:\\yijie\\"+"wrt_"+folwId+"_"+userId+"_.wav";

        int byteRead;
        URL url;
        try {
            url = new URL(path);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            return "";
        }
        try {
            //2.获取链接
            URLConnection conn = url.openConnection();
            //3.输入流
            InputStream inStream = conn.getInputStream();
            //3.写入文件
            FileOutputStream fs = new FileOutputStream(localPath);
            byte[] buffer = new byte[1024];
            while ((byteRead = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteRead);
            }
            inStream.close();
            fs.close();
            return localPath;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void main(String[] args) {
        String localPath = getLocalPath("https://andi159753.oss-cn-hangzhou.aliyuncs.com/sz/ly.wav"
                , "3", "4");
        System.out.println(localPath);
    }
}
